/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.appspot.ittrick.drive.trick.factory;

import com.appspot.ittrick.drive.trick.model.FileModel;
import javafx.event.EventHandler;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.util.Callback;

/**
 *
 * @author imamura
 */
public class FileTableViewTableRowFactory implements Callback<TableView, TableRow> {

    @Override
    public TableRow call(TableView p) {
        final TableRow tableRow = new TableRow();
        
        // ---------------------------------------------------------------------
        // ドラッグ 開始
        // ---------------------------------------------------------------------
        tableRow.setOnDragDetected(
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {

                        FileModel fileModel = (FileModel) tableRow.getItem();

                        // ドラッグボードの設定
                        Dragboard db = tableRow.startDragAndDrop(TransferMode.MOVE);
                        ClipboardContent clipd = new ClipboardContent();
                        clipd.putString(fileModel.getFile().getId());
                        db.setContent(clipd);

                        event.consume();
                    }
                });

        // ---------------------------------------------------------------------
        // ドラッグが実行された時
        // ---------------------------------------------------------------------
        tableRow.setOnDragDone(
                new EventHandler<DragEvent>() {
                    @Override
                    public void handle(DragEvent event) {
                        
                        if (event.getDragboard().getString().equals("DragSuccess")) {
                            TableRow targetTableRow = (TableRow) event.getTarget();
                            targetTableRow.getTableView().getItems().remove(targetTableRow.getItem());
                        }
                        
                        event.consume();
                    }
                });
        
        return tableRow;
    }
}
